package com.servlet;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Transaction;

import com.hibernate.dao.HibernateSessionFactory;
import com.hibernate.model.Basicinfo;
import com.hibernate.model.BasicinfoDAO;
import com.hibernate.model.Worker;
import com.hibernate.model.WorkerDAO;
import com.servlet.util.Cookie_util;

public class User_info extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public User_info() {
		super();
	}
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	
		
	       // Cookie_util ck=new Cookie_util(request);
	       // String userid=ck.getValueByname("userid");
		String userid=(String)request.getSession().getAttribute("userid");
	        Basicinfo info=null;
	        Worker wk_info=null;
	        if(userid!=null)
	        {
	        	BasicinfoDAO op=new BasicinfoDAO();
	            Transaction se;
	          	  
	       		try{
	           		se= HibernateSessionFactory.getSession().beginTransaction();
	           		info =op.findById(userid);
	           		 se.commit();
	           	 }catch( Exception e){
	           		 HibernateSessionFactory.getSession().getTransaction().rollback();
	           	 }
	           	 finally {
	           		 HibernateSessionFactory.closeSession();
	           	 }
	            if(info!=null)
		        {
		         
		         String status	=info.getStatus();
		         if(status.equals("stu"))
		         {   info.setStatus("普通会员");
		        	 request.setAttribute("user_info", info);
		        	 request.setAttribute("status", "stu");
		        	 System.out.println("stu conme in");
		         }
		         else if(status.equals("stuteam"))
		         {   info.setStatus("学生团队");
	        	 request.setAttribute("user_info", info);
	        	 request.setAttribute("status", "stuteam");
	        	 System.out.println("stuteam conme in");
	            }
		         else if(status.equals("worker"))
		         {   info.setStatus("在岗学生");
	        	 request.setAttribute("user_info", info);
	        	 request.setAttribute("status", "worker");
	        	 
	        	 WorkerDAO opwk=new WorkerDAO();
		          
		          	  
		       		try{
		           		se= HibernateSessionFactory.getSession().beginTransaction();
		           		 wk_info=opwk.findById(userid);
		           		 se.commit();
		           	 }catch( Exception e){
		           		 HibernateSessionFactory.getSession().getTransaction().rollback();
		           	 }
		           	 finally {
		           		 HibernateSessionFactory.closeSession();
		           	 }
		       	  request.setAttribute("wk_info", wk_info);
	        
	            }
		       
		         request.getRequestDispatcher("/user_info.jsp").forward(request, response);
		        }
		       else
		        {
		        	System.out.println("非法用户");
		        }	
	       		
	        	
	        }  
	        else{
	        	response.sendRedirect("/stusys/index.jsp");
	        	//yo用户未登录的情况
	        	
	        }
	        
	        
	   
		
	}
}
